package com.android.kmlmrg.utils;

import com.android.kmlmrg.dao.ContactDao;
import com.android.kmlmrg.model.User;
import com.android.kmlmrg.services.ServiceController;
import com.android.kmlmrg.views.AddOrUpdateContact;
import com.android.kmlmrg.views.Search;
import com.android.kmlmrg.views.ViewContact;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;

public class MenuBarHelper implements Const {
	/**LOG**/
	private static final  String LOG_TAG = MenuBarHelper.class.getName();
	
	public static boolean manageMenuItemClick(MenuItem item, Activity vContext) {
		Log.i(LOG_TAG, "START -  manageMenuItemClick(MenuItem item, Activity vContext) ");
		
		Intent vIntent = null;
		TextView num = null;
		String data = null;
		ServiceController serviceController = ServiceController.getInstance();
		switch (item.getItemId()) {
			case android.R.id.home:
				Log.i(LOG_TAG, "ACTION HOME ");	
				vContext.onBackPressed();
				break;

			case com.android.kmlmrg.R.id.manuel:
				Log.i(LOG_TAG, "ACTION MANUEL ");	
				vIntent = new Intent(vContext , AddOrUpdateContact.class);
				vIntent.putExtra("isForAdd", true);
				vIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
				vContext.startActivity(vIntent);
				
				break;
			case com.android.kmlmrg.R.id.fromContact:
				Log.i(LOG_TAG, "ACTION FROM CONTACT ");	
				Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
				vContext.startActivityForResult(intent, PICK_CONTACT);
				
				break;

			case com.android.kmlmrg.R.id.itemSearch:
				Log.i(LOG_TAG, "ACTION ITEM SEARCH ");	
				vIntent = new Intent(vContext , Search.class);
				vIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
				vContext.startActivity(vIntent);
				break;
			case com.android.kmlmrg.R.id.itemReport:
				Log.i(LOG_TAG, "ACTION ITEM REPORT ");	
				serviceController.printReport();
				
				break;
				
			case com.android.kmlmrg.R.id.itemDeconnexion:
				Log.i(LOG_TAG, "ACTION ITEM DECONNEXION ");		
				vContext.onBackPressed();
				break;
				
			case com.android.kmlmrg.R.id.call:
				Log.i(LOG_TAG, "ACTION CALL ");
				num =(TextView)vContext.findViewById(com.android.kmlmrg.R.id.viewContactNumber);
				data =  num.getText().toString().trim();
				AndroidFunctionHelper.call(vContext, vIntent, data);
				break;
				

			case com.android.kmlmrg.R.id.itemPrint:
				Log.i(LOG_TAG, "ACTION ITEM PRINT ");
				serviceController.printReport();
				break;
				
			case com.android.kmlmrg.R.id.sendMail:
				Log.i(LOG_TAG, "ACTION SEND MAIL ");
				data = ((TextView)vContext.findViewById(com.android.kmlmrg.R.id.viewContactEmail)).getText().toString().trim();
				AndroidFunctionHelper.sendEmail(vContext, vIntent, data, STRING_EMPTY, STRING_EMPTY);
				break;
				
			case com.android.kmlmrg.R.id.sendSMS:
				Log.i(LOG_TAG, "ACTION SEND SMS");
				num =(TextView)vContext.findViewById(com.android.kmlmrg.R.id.viewContactNumber);
				data = num.getText().toString().trim();
				AndroidFunctionHelper.sendSMS(vContext, vIntent, data, STRING_EMPTY);
				
				break;	
			
			case com.android.kmlmrg.R.id.deleteProfil:
				Log.i(LOG_TAG, "ACTION DELETE PROFIL");
				
				((ViewContact)vContext).launchDeleteProfil();
				break;
				
			case com.android.kmlmrg.R.id.addInContats:
				Log.i(LOG_TAG, "ACTION ADD IN CONTACT ");	
				ContactDao contact = new ContactDao();
				ViewContact v = (ViewContact) vContext;
				contact.addInContact(v, v.getUser());
				break;
			case com.android.kmlmrg.R.id.updatePhoto:
				Log.i(LOG_TAG, "ACTION UPDATE PHOTO ");	
				ViewContact viewContact = (ViewContact) vContext;
				viewContact.lauchPikerImage();
				break;
			case com.android.kmlmrg.R.id.updateProfil:
				Log.i(LOG_TAG, "ACTION EDIT PROFIL ");	
				ViewContact viewC = (ViewContact) vContext;
				viewC.launchEditProfil();
				break;
			case com.android.kmlmrg.R.id.itemValid:
				
				AddOrUpdateContact vAddContact = (AddOrUpdateContact)vContext;
				User user = vAddContact.instanciateUser();
				if(vAddContact.validation()){
					if(vAddContact.isForAdd()){
						Log.i(LOG_TAG, "ACTION NEW CONTACT ");
						serviceController.addUserServ(user);
					}
					else{
						Log.i(LOG_TAG, "ACTION UPDATE CONTACT ");
						vAddContact.updateProfil();
					}
					vContext.onBackPressed();
				}
				break;
			case com.android.kmlmrg.R.id.itemCancel:
				vContext.onBackPressed();
				break;
			default:
				break;
		}
		Log.i(LOG_TAG, "START -  manageMenuItemClick RETURN TRUE ");
		return true;
		
		
	}
	
}
